﻿#include "scannercontrol.h"


ScannerControl::ScannerControl(QObject *parent)
    : QObject{parent}
{
    m_Serial = new QSerialPort(this);
}

void ScannerControl::setConnectParameter(QString serial,
                                         QString baudrate,
                                         QString databit,
                                         QString paritybit,
                                         QString stopbit)
{

    QSerialPort::BaudRate set_BaudRate;
    QSerialPort::DataBits set_DataBit;
    QSerialPort::Parity   set_ParityBit;
    QSerialPort::StopBits set_StopBit;

    // 设置 - 波特率
    if("115200" == baudrate){
        set_BaudRate = QSerialPort::Baud115200;
    }else if("57600" == baudrate){
        set_BaudRate = QSerialPort::Baud57600;
    }else if("38400" == baudrate){
        set_BaudRate = QSerialPort::Baud38400;
    }else if("19200" == baudrate){
        set_BaudRate = QSerialPort::Baud19200;
    }else if("9600" == baudrate){
        set_BaudRate = QSerialPort::Baud9600;
    }else if("4800" == baudrate){
        set_BaudRate = QSerialPort::Baud4800;
    }else if("2400" == baudrate){
        set_BaudRate = QSerialPort::Baud2400;
    }else {
        set_BaudRate = QSerialPort::Baud1200;
    }

    // 设置 - 数据位
    if("8" == databit){
        set_DataBit = QSerialPort::Data8;
    }else if("7" == databit){
        set_DataBit = QSerialPort::Data7;
    }else if("6" == databit){
        set_DataBit = QSerialPort::Data6;
    }else {
        set_DataBit = QSerialPort::Data5;
    }

    // 设置 - 校验位
    if("None" == paritybit){
        set_ParityBit = QSerialPort::NoParity;
    }else if("Even" == paritybit){
        set_ParityBit = QSerialPort::EvenParity;
    }else if("Odd" == paritybit){
        set_ParityBit = QSerialPort::OddParity;
    }else if("Mark" == paritybit){
        set_ParityBit = QSerialPort::MarkParity;
    }else {
        set_ParityBit = QSerialPort::SpaceParity;
    }

    // 设置 - 停止位
    if("1" == stopbit){
        set_StopBit = QSerialPort::OneStop;
    }else if("1.5" == stopbit){
        set_StopBit = QSerialPort::OneAndHalfStop;
    }else{
        set_StopBit = QSerialPort::TwoStop;
    }

    m_Serial->setPortName(serial);
    m_Serial->setBaudRate(set_BaudRate);
    m_Serial->setDataBits(set_DataBit);
    m_Serial->setParity(set_ParityBit);
    m_Serial->setStopBits(set_StopBit);

}

void ScannerControl::connectMethod()
{
    if(m_Serial->open(QIODevice::ReadWrite))
    {
        qDebug()<<"Cpp : 扫码枪 连接成功";
        // setLinkState(true);
    }
    else
    {
        qDebug()<<"Cpp : 扫码枪 连接失败";
        // setLinkState(false);
    }
}


